package com.hsmw.api.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.controller.pub.TencentPushUtils;
import com.hsmw.api.mapper.*;
import com.hsmw.api.vo.WebSocketReturnVO;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.util.SendSmsUtil;
import com.hsmw.api.utils.CommonUtils;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.*;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.htn.common.core.constant.CommonConstants.NO;
import static com.htn.common.core.constant.CommonConstants.ResponseStatus.*;


/**
 * @author WD
 */
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwInviteRecordService extends BaseService<HsmwInviteRecordMapper, HsmwInviteRecord> {

    @Autowired
    private HsmwInviteRecordMapper hsmwInviteRecordMapper;

    @Autowired
    private HsmwInviteRecordService hsmwInviteRecordService;


    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    @Autowired
    private HsmwHtnUserService hsmwHtnUserService;


    @Autowired
    private HsmwPushInfoService hsmwPushInfoService;

    @Autowired
    private HsmwDriverNewsService hsmwDriverNewsService;

    @Autowired
    private HsmwLeasingCompanyMapper hsmwLeasingCompanyMapper;


    @Autowired
    private HsmwCorporationCompanyMapper hsmwCorporationCompanyMapper;


    @Autowired
    private HsmwForcemanItemMapper hsmwForcemanItemMapper;


    @Autowired
    private HsmwOrderMapper hsmwOrderMapper;

    @Autowired
    private HsmwItemConcreteRelationService hsmwItemConcreteRelationService;

    @Autowired
    private HsmwItemConcreteRelationMapper hsmwItemConcreteRelationMapper;



    @Autowired
    private HsmwConcretePlantInfoMapper hsmwConcretePlantInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Value(value = "${sms.appKey}")
    private String appKey;
    @Value(value = "${sms.channel}")
    private String channel;
    @Value(value = "${sms.smsSign}")
    private String smsSign;
    @Value(value = "${sms.appId}")
    private int appId;

    //搅拌站邀请自营公司
    @Value(value = "${sms.concreteInviteSelfrun}")
    private int concreteInviteSelfrun;

    //搅拌站邀请合作公司
    @Value(value = "${sms.concreteInviteCorperate}")
    private int concreteInviteCorperate;

    //泵送公司邀请合作公司
    @Value(value = "${sms.companyInviteCorperate}")
    private int companyInviteCorperate;

    //搅拌站或者租赁公司的额度不足
    @Value(value = "${sms.haveNoBalance}")
    private int haveNoBalance;


    //泵送公司邀请工长
    @Value(value = "${sms.companyInviteForeman}")
    private int companyInviteForeman;

    //泵送公司邀请工长
    @Value(value = "${sms.companyInviteProjectman}")
    private int companyInviteProjectman;

    //搅拌站邀请工长
    @Value(value = "${sms.concreteInviteForeman}")
    private int concreteInviteForeman;

    //搅拌站邀请项目负责人
    @Value(value = "${sms.concreteInviteProjectman}")
    private int concreteInviteProjectman;

    //搅拌站邀请项目负责人
    @Value(value = "${sms.projectmanInviteForceman}")
    private int projectmanInviteForceman;




    //邀请调度
    @Value(value = "${sms.inviteDispatch}")
    private int inviteDispatch;


    //项目邀请搅拌站
    @Value(value = "${sms.itemInviteConcrete}")
    private int itemInviteConcrete;


    public DataResponse saveOrUpdateInviteInfo(HsmwInviteRecord record) {
        String inviteeUserId = record.getInviteeUserId();
        //当前用户吧
        LoginUser currentHtnUser = currentHsmwUserUtils.getCurrentUserInfo();
        HsmwHtnUser hsmwHtnUser = currentHsmwUserUtils.getCurrentHtnUserInfo();
        //公司邀请 搅拌站 公司邀请个人的话 都必须是普通用户
        if (InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue().equals(record.getInviteType())
                || InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(record.getInviteType())) {
            //如果被邀请人的电话不为空 那么说明是单独邀请 只邀请一个人
            if (StringUtils.isNotBlank(record.getInviteeUserTel())) {
                //邀请成为项目的调度  用户只能是普通用户
                LambdaQueryWrapper<HsmwHtnUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.in(HsmwHtnUser::getType,
                        HtnUserTypeEnum.COMPANY.getValue(),
                        HtnUserTypeEnum.PLATFORM.getValue(),
                        HtnUserTypeEnum.CONCRETE.getValue(),
                        HtnUserTypeEnum.FOREMAN.getValue());
                lambdaQueryWrapper.eq(HsmwHtnUser::getUsername, record.getInviteeUserTel());
                List<HsmwHtnUser> userList = hsmwHtnUserService.list(lambdaQueryWrapper);
                if (CollectionUtil.isNotEmpty(userList)) {
                    return new DataResponse(CommonConstants.ResponseStatus.REGISTER_OTHER_ROLE, "该账号已有其他角色！");
                }
                List<HsmwHtnUser> normalList = hsmwHtnUserService.list(new LambdaQueryWrapper<HsmwHtnUser>()
                        .in(HsmwHtnUser::getType, HtnUserTypeEnum.NORMAL.getValue())
                        .eq(HsmwHtnUser::getUsername, record.getInviteeUserTel()));
                if (CollectionUtil.isEmpty(normalList)) {
                    return new DataResponse(CommonConstants.ResponseStatus.UN_REGISTER, "该账号暂未注册");
                } else {
                    HsmwHtnUser inviteeUser = normalList.get(0);
                    record.setInviteUserId(currentHtnUser.getId());
                    record.setInviteUserName(hsmwHtnUser.getName());
                    record.setInviteUserTel(hsmwHtnUser.getUsername());
                    record.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                    record.setInviteeUserId(inviteeUser.getId());
                    //邀请人的公司ID 和name
                    record.setInviteCompanyId(hsmwHtnUser.getOwnCompanyId());
                    record.setInviteCompanyName(hsmwHtnUser.getOwnCompanyName());

                    String recordId = CommonUtils.generateId(record);
                    record.setId(recordId);
                    //添加推送的信息
                    HsmwPushInfo pushInfo = new HsmwPushInfo();
                    HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                    String message = "";
                    String pushMessage = "";
                    pushInfo.setInvitedId(recordId);
                    pushInfo.setForemanId(inviteeUser.getId());
                    pushInfo.setInvitedUserName(currentHtnUser.getUsername());
                    pushInfo.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                    pushInfo.setInvitedItemName(record.getItemName());
                    //公司邀请调度  系统通知
                    if (InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue().equals(record.getInviteType())) {
                        //如果已经存在该用户的邀请信息
                        List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                .eq(HsmwInviteRecord::getInviteeUserTel, record.getInviteeUserTel())
                                .eq(HsmwInviteRecord::getInviteType, InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue())
                                .eq(HsmwInviteRecord::getInviteUserId, currentHtnUser.getId()));
                        if (CollectionUtil.isNotEmpty(records)) {
                            HsmwInviteRecord inviteRecord = records.get(0);
                            //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                            if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                            }
                            //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                            if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                hsmwInviteRecordMapper.updateById(inviteRecord);
                                return new DataResponse(SUCCESS, "邀请成功！");
                            }
                            //当出现已经邀请成功 的记录时 ， 不进行任何操作
                            if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(USER_EXISTS, "该用户已存在，无需重复邀请！");
                            }
                        }
                        message = "有公司邀请您加入，请及时查看";
                        HsmwLeasingCompany company = hsmwLeasingCompanyMapper.selectById(hsmwHtnUser.getOwnCompanyId());
                        pushInfo.setType(PushTypeEnum.COMPANY_INVITE_DISPATCH.getValue());
                        pushMessage = "用户" + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您加入公司";
                        String[] params = new String[]{company.getName(), hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername()};
                        String pushId = CommonUtils.generateId(pushInfo);
                        pushInfo.setId(pushId);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        //webSocketReturnVO.setInvitedCompanyName(one.getInvitedCompanyName());
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_DISPATCH.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        //webSocketReturnVO.setItemName(one.getInvitedItemName());
                        TencentPushUtils.htnPushInviteMsg(message, record.getInviteeUserTel(), webSocketReturnVO);
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, inviteDispatch, smsSign, record.getInviteeUserTel());
                    }
                    //搅拌站邀请调度  系统通知
                    if (InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(record.getInviteType())) {
                        //如果已经存在该用户的邀请信息
                        List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                .eq(HsmwInviteRecord::getInviteeUserTel, record.getInviteeUserTel())
                                .eq(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue())
                                .eq(HsmwInviteRecord::getItemId, record.getItemId()));
                        if (CollectionUtil.isNotEmpty(records)) {
                            HsmwInviteRecord inviteRecord = records.get(0);
                            //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                            if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                            }
                            //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                            if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                hsmwInviteRecordMapper.updateById(inviteRecord);
                                return new DataResponse(SUCCESS, "邀请成功！");
                            }
                            //当出现已经邀请成功 的记录时 ， 不进行任何操作
                            if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(USER_EXISTS, "该用户已存在，无需重复邀请！");
                            }
                        }

                        message = "有公司邀请您加入，请及时查看";
                        pushInfo.setType(PushTypeEnum.CONCRETE_INVITE_DISPATCH.getValue());
                        pushMessage = "用户" + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您加入搅拌站";
                        String[] params = new String[]{hsmwHtnUser.getOwnCompanyName(), hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername()};
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, inviteDispatch, smsSign, record.getInviteeUserTel());
                        String pushId = CommonUtils.generateId(pushInfo);
                        pushInfo.setId(pushId);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        //webSocketReturnVO.setInvitedCompanyName(one.getInvitedCompanyName());
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_DISPATCH.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        //webSocketReturnVO.setItemName(one.getInvitedItemName());
                        TencentPushUtils.htnPushInviteMsg(message, record.getInviteeUserTel(), webSocketReturnVO);

                    }
                    hsmwInviteRecordMapper.insert(record);
                    pushInfo.setMessage(pushMessage);
                    hsmwPushInfoService.save(pushInfo);
                    hsmwDriverNews.setMessage(pushMessage);
                    hsmwDriverNews.setUserId(record.getInviteeUserId());
                    hsmwDriverNews.setAlreadyRead(NO);
                    hsmwDriverNewsService.save(hsmwDriverNews);
                    return new DataResponse(SUCCESS, "邀请成功");
                }
            }
        }
        if (InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue().equals(record.getInviteType()) || InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue().equals(record.getInviteType()) ||
                InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue().equals(record.getInviteType()) || InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())
                || InviteTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue().equals(record.getInviteType())
        ) {
            //如果被邀请人的电话不为空 那么说明是单独邀请 只邀请一个人
            if (StringUtils.isNotBlank(record.getInviteeUserTel())) {
                //邀请成为项目的工长或者负责人 用户不能是 公司用户、搅拌站用户
                LambdaQueryWrapper<HsmwHtnUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.in(HsmwHtnUser::getType,
                        HtnUserTypeEnum.COMPANY.getValue(),
                        HtnUserTypeEnum.CONCRETE.getValue());
                lambdaQueryWrapper.eq(HsmwHtnUser::getUsername, record.getInviteeUserTel());
                List<HsmwHtnUser> userList = hsmwHtnUserService.list(lambdaQueryWrapper);
                if (CollectionUtil.isNotEmpty(userList)) {
                    return new DataResponse(CommonConstants.ResponseStatus.REGISTER_OTHER_ROLE, "该账号已有其他角色！");
                }
                List<HsmwHtnUser> normalList = hsmwHtnUserService.list(new LambdaQueryWrapper<HsmwHtnUser>()
                        .in(HsmwHtnUser::getType, HtnUserTypeEnum.NORMAL.getValue(), HtnUserTypeEnum.FOREMAN.getValue())
                        .eq(HsmwHtnUser::getUsername, record.getInviteeUserTel()));
                if (CollectionUtil.isEmpty(normalList)) {
                    return new DataResponse(CommonConstants.ResponseStatus.UN_REGISTER, "该账号暂未注册");
                } else {
                    HsmwHtnUser inviteeUser = normalList.get(0);
                    record.setInviteUserId(currentHtnUser.getId());
                    record.setInviteUserName(currentHtnUser.getName());
                    record.setInviteUserTel(currentHtnUser.getUsername());
                    record.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                    record.setInviteeUserId(inviteeUser.getId());
                    String recordId = CommonUtils.generateId(record);
                    //邀请人的公司ID 和name
                    record.setInviteCompanyId(hsmwHtnUser.getOwnCompanyId());
                    record.setInviteCompanyName(hsmwHtnUser.getOwnCompanyName());
                    record.setId(recordId);
                    //添加推送的信息
                    HsmwPushInfo pushInfo = new HsmwPushInfo();
                    HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                    String message = "";
                    String pushMessage = "";
                    if (InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue().equals(record.getInviteType())) {
                        //如果已经存在该用户的邀请信息
                        List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                .eq(HsmwInviteRecord::getInviteeUserTel, record.getInviteeUserTel())
                                .eq(HsmwInviteRecord::getItemId, record.getItemId())
                                .in(HsmwInviteRecord::getInviteStatus,InviteStatusEnum.ON_PROCESS.getValue(),InviteStatusEnum.RECEIVED.getValue())
                                .in(HsmwInviteRecord::getInviteType, InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue(), InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue()));
                        if (CollectionUtil.isNotEmpty(records)) {
                            HsmwInviteRecord inviteRecord = records.get(0);
                            //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                            if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                            }
                            //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                            if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                hsmwInviteRecordMapper.updateById(inviteRecord);
                                return new DataResponse(SUCCESS, "邀请成功！");
                            }
                            //当出现已经邀请成功 的记录时 ， 不进行任何操作
                            if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(USER_EXISTS, "该用户已存在，无需重复邀请！");
                            }
                        }

                        //XXXXX（角色，泵送公司负责人或项目负责人或项目经理）用户XXXX（姓名）邀请您加入项目XXXXX（项目名称），成为项目的XXXX（项目用户的角色）
                        message = "泵送公司" + hsmwHtnUser.getOwnCompanyName() + "的用户 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您加入项目" + record.getItemName() + "，成为项目的工长";
                        pushInfo.setInvitedItemName(record.getItemName());
                        pushInfo.setType(PushTypeEnum.COMPANY_INVITE_FOREMAN.getValue());
                        pushMessage = "泵送公司" + hsmwHtnUser.getOwnCompanyName() + "用户 " + hsmwHtnUser.getName() + hsmwHtnUser.getUsername() + "邀请您加入项目，成为项目工长";
                        String[] params = new String[]{hsmwHtnUser.getName(), record.getItemName()};
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, companyInviteForeman, smsSign, record.getInviteeUserTel());
                        String pushId = CommonUtils.generateId(pushInfo);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_FOREMAN.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        webSocketReturnVO.setItemName(record.getItemName());
                        webSocketReturnVO.setInviteeName(record.getInviteeUserName());
                        TencentPushUtils.htnPushInviteMsg(pushMessage, record.getInviteeUserTel(), webSocketReturnVO);
                        pushInfo.setId(pushId);

                    }
                    if (InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())) {
                        //如果已经存在该用户的邀请信息
                        //如果已经存在该用户的邀请信息
                        List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                .eq(HsmwInviteRecord::getInviteeUserTel, record.getInviteeUserTel())
                                .eq(HsmwInviteRecord::getItemId, record.getItemId())
                                .in(HsmwInviteRecord::getInviteStatus,InviteStatusEnum.ON_PROCESS.getValue(),InviteStatusEnum.RECEIVED.getValue())
                                .in(HsmwInviteRecord::getInviteType, InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue(),InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue()));
                        if (CollectionUtil.isNotEmpty(records)) {
                            HsmwInviteRecord inviteRecord = records.get(0);
                            //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                            if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                            }
                            //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                            if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                hsmwInviteRecordMapper.updateById(inviteRecord);
                                return new DataResponse(SUCCESS, "邀请成功！");
                            }
                            //当出现已经邀请成功 的记录时 ， 不进行任何操作
                            if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(USER_EXISTS, "该用户已存在，无需重复邀请！");
                            }
                        }


                        //XXXXX（角色，泵送公司负责人或项目负责人或项目经理）用户XXXX（姓名）邀请您加入项目XXXXX（项目名称），成为项目的XXXX（项目用户的角色）
                        message = "泵送公司" + hsmwHtnUser.getOwnCompanyName() + "的用户 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您加入项目" + record.getItemName() + "，成为项目的工长";
                        pushInfo.setInvitedItemName(record.getItemName());
                        pushInfo.setType(PushTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue());
                        pushMessage = "泵送公司" + hsmwHtnUser.getOwnCompanyName() + "用户 " + hsmwHtnUser.getName() + hsmwHtnUser.getUsername() + "邀请您加入项目，成为项目负责人";
                        String[] params = new String[]{hsmwHtnUser.getName(), record.getItemName()};
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, companyInviteProjectman, smsSign, inviteeUser.getUsername());
                        String pushId = CommonUtils.generateId(pushInfo);
                        pushInfo.setId(pushId);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        webSocketReturnVO.setItemName(record.getItemName());
                        webSocketReturnVO.setInviteeName(record.getInviteeUserName());
                        TencentPushUtils.htnPushInviteMsg(pushMessage, record.getInviteeUserTel(), webSocketReturnVO);
                    }
                    if (InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue().equals(record.getInviteType())) {
                        //如果已经存在该用户的邀请信息
                        List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                .eq(HsmwInviteRecord::getInviteeUserTel, record.getInviteeUserTel())
                                .eq(HsmwInviteRecord::getItemId, record.getItemId())
                                .in(HsmwInviteRecord::getInviteStatus,InviteStatusEnum.ON_PROCESS.getValue(),InviteStatusEnum.RECEIVED.getValue())
                                .in(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue(),InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue()));
                        if (CollectionUtil.isNotEmpty(records)) {
                            HsmwInviteRecord inviteRecord = records.get(0);
                            //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                            if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                            }
                            //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                            if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                hsmwInviteRecordMapper.updateById(inviteRecord);
                                return new DataResponse(SUCCESS, "邀请成功！");
                            }
                            //当出现已经邀请成功 的记录时 ， 不进行任何操作
                            if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(USER_EXISTS, "该用户已存在，无需重复邀请！");
                            }
                        }

                        //XXXXX（角色，泵送公司负责人或项目负责人或项目经理）用户XXXX（姓名）邀请您加入项目XXXXX（项目名称），成为项目的XXXX（项目用户的角色）
                        message = "搅拌站" + hsmwHtnUser.getOwnCompanyName() + "的用户 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您加入项目" + record.getItemName() + "，成为项目的工长";
                        pushInfo.setInvitedItemName(record.getItemName());
                        pushInfo.setType(PushTypeEnum.CONCRETE_INVITE_FOREMAN.getValue());
                        pushMessage = "搅拌站" + hsmwHtnUser.getOwnCompanyName() + "用户 " + hsmwHtnUser.getName() + hsmwHtnUser.getUsername() + "邀请您加入项目，成为项目工长";
                        String[] params = new String[]{hsmwHtnUser.getName(),record.getItemName()};
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, concreteInviteForeman, smsSign, inviteeUser.getUsername());
                        String pushId = CommonUtils.generateId(pushInfo);
                        pushInfo.setId(pushId);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_FOREMAN.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        webSocketReturnVO.setItemName(record.getItemName());
                        webSocketReturnVO.setInviteeName(record.getInviteeUserName());
                        TencentPushUtils.htnPushInviteMsg(pushMessage, record.getInviteeUserTel(), webSocketReturnVO);

                    }
                    if (InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())) {
                        //如果已经存在该用户的邀请信息
                        List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                .eq(HsmwInviteRecord::getInviteeUserTel, record.getInviteeUserTel())
                                .eq(HsmwInviteRecord::getItemId, record.getItemId())
                                .in(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue(),InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue())
                                .in(HsmwInviteRecord::getInviteStatus,InviteStatusEnum.ON_PROCESS.getValue(),InviteStatusEnum.RECEIVED.getValue()));
                        if (CollectionUtil.isNotEmpty(records)) {
                            HsmwInviteRecord inviteRecord = records.get(0);
                            //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                            if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                            }
                            //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                            if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                hsmwInviteRecordMapper.updateById(inviteRecord);
                                return new DataResponse(SUCCESS, "邀请成功！");
                            }
                            //当出现已经邀请成功 的记录时 ， 不进行任何操作
                            if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(USER_EXISTS, "该用户已存在，无需重复邀请！");
                            }
                        }
                        //XXXXX（角色，泵送公司负责人或项目负责人或项目经理）用户XXXX（姓名）邀请您加入项目XXXXX（项目名称），成为项目的XXXX（项目用户的角色）
                        message = "搅拌站" + hsmwHtnUser.getOwnCompanyName() + "的用户 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您加入项目" + record.getItemName() + "，成为项目负责人";
                        pushInfo.setInvitedItemName(record.getItemName());
                        pushInfo.setType(PushTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue());
                        pushMessage = "搅拌站" + hsmwHtnUser.getOwnCompanyName() + "用户 " + hsmwHtnUser.getName() + hsmwHtnUser.getUsername() + "邀请您加入项目" + record.getItemName()+"，成为项目负责人";
                        String[] params = new String[]{hsmwHtnUser.getName(),record.getItemName()};
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, concreteInviteProjectman, smsSign, inviteeUser.getUsername());
                        String pushId = CommonUtils.generateId(pushInfo);
                        pushInfo.setId(pushId);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        webSocketReturnVO.setItemName(record.getItemName());
                        webSocketReturnVO.setInviteeName(record.getInviteeUserName());
                        TencentPushUtils.htnPushInviteMsg(pushMessage, record.getInviteeUserTel(), webSocketReturnVO);
                    }
                    if (InviteTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue().equals(record.getInviteType())) {
                        //如果已经存在该用户的邀请信息
                        List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                .eq(HsmwInviteRecord::getInviteeUserTel, record.getInviteeUserTel())
                                .eq(HsmwInviteRecord::getItemId, record.getItemId())
                                .in(HsmwInviteRecord::getInviteType,
                                        InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue(),
                                        InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue(),
                                        InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue(),
                                        InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue(),
                                        InviteTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue())
                                .in(HsmwInviteRecord::getInviteStatus,InviteStatusEnum.ON_PROCESS.getValue(),InviteStatusEnum.RECEIVED.getValue()));
                        if (CollectionUtil.isNotEmpty(records)) {
                            HsmwInviteRecord inviteRecord = records.get(0);
                            //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                            if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                            }
                            //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                            if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                hsmwInviteRecordMapper.updateById(inviteRecord);
                                return new DataResponse(SUCCESS, "邀请成功！");
                            }
                            //当出现已经邀请成功 的记录时 ， 不进行任何操作
                            if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                return new DataResponse(USER_EXISTS, "该用户已存在，无需重复邀请！");
                            }
                        }
                        //XXXXX（项目名称）项目的项目负责人XXXX（姓名电话）邀请您成为该项目工长”。
                        message = record.getItemName()+"项目的项目负责人" + hsmwHtnUser.getName() +" "+ hsmwHtnUser.getUsername() + "邀请您成为该项目工长!";
                        pushInfo.setInvitedItemName(record.getItemName());
                        pushInfo.setType(PushTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue());
                        pushMessage = record.getItemName()+"项目的项目负责人" + hsmwHtnUser.getName() +" "+ hsmwHtnUser.getUsername() + "邀请您成为该项目工长!";
                        String[] params = new String[]{record.getItemName(),hsmwHtnUser.getName() +" "+ hsmwHtnUser.getUsername(),};
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, projectmanInviteForceman, smsSign, inviteeUser.getUsername());
                        String pushId = CommonUtils.generateId(pushInfo);
                        pushInfo.setId(pushId);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        webSocketReturnVO.setItemName(record.getItemName());
                        webSocketReturnVO.setInviteeName(record.getInviteeUserName());
                        TencentPushUtils.htnPushInviteMsg(pushMessage, record.getInviteeUserTel(), webSocketReturnVO);
                    }

                    hsmwInviteRecordMapper.insert(record);
                    pushInfo.setMessage(pushMessage);
                    pushInfo.setInvitedId(recordId);
                    pushInfo.setForemanId(inviteeUser.getId());
                    pushInfo.setInvitedUserName(currentHtnUser.getUsername());
                    pushInfo.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                    pushInfo.setInvitedItemName(record.getItemName());

                    hsmwPushInfoService.save(pushInfo);

                    hsmwDriverNews.setMessage(pushMessage);
                    hsmwDriverNews.setUserId(record.getInviteeUserId());
                    hsmwDriverNews.setAlreadyRead(NO);
                    hsmwDriverNewsService.save(hsmwDriverNews);
                    return new DataResponse(SUCCESS, "邀请成功");
                }
            }

        }

        if (InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue().equals(record.getInviteType()) || InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue().equals(record.getInviteType())) {
            if (CollectionUtil.isNotEmpty(record.getUserIds())) {

                List<HsmwInviteRecord> records1 = new ArrayList<>();
                List<HsmwDriverNews> nesss = new ArrayList<>();
                String message = "";
                String pushMessage = "";
                if (InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue().equals(record.getInviteType())) {
                    List<HsmwPushInfo> pushInfos = new ArrayList<>();
                    //泵送公司邀请的是多个合作公司
                    List<String> userIds = record.getUserIds();
                    if (CollectionUtil.isNotEmpty(userIds)) {
                        //泵送公司邀请的是多个合作公司
                        if (CollectionUtil.isNotEmpty(userIds)) {
                            for (String userId : userIds) {
                                HsmwHtnUser companyUser = hsmwHtnUserService.getById(userId);
                                //如果已经存在该用户的邀请信息
                                List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                        .in(HsmwInviteRecord::getInviteeUserId, userId)
                                        .eq(HsmwInviteRecord::getInviteCompanyId, companyUser.getOwnCompanyId())
                                        .eq(HsmwInviteRecord::getInviteType, InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue())
                                        .eq(HsmwInviteRecord::getInviteUserId, currentHtnUser.getId()));
                                if (CollectionUtil.isNotEmpty(records)) {
                                    HsmwInviteRecord inviteRecord = records.get(0);
                                    //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                                    if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                        return new DataResponse(SUCCESS, "邀请成功！");
                                    }
                                    //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                                    if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                        inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                        hsmwInviteRecordMapper.updateById(inviteRecord);
                                        return DataResponse.success(CommonConstants.ResponseStatus.INVITE_SUCCESS.getCode());
                                    }
                                    //当出现已经邀请成功 的记录时 ， 不进行任何操作
                                    if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                        return new DataResponse(SUCCESS, "邀请成功！");
                                    }
                                }
                                //添加推送的信息
                                HsmwPushInfo pushInfo = new HsmwPushInfo();
                                HsmwInviteRecord record1 = new HsmwInviteRecord();
                                BeanUtil.copyProperties(record, record1);
                                record1.setInviteUserId(currentHtnUser.getId());
                                record1.setInviteType(InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue());
                                record1.setInviteUserName(currentHtnUser.getName());
                                record1.setInviteUserTel(currentHtnUser.getUsername());
                                record1.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                record1.setInviteType(InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue());
                                record1.setInviteeUserId(userId);
                                record1.setInviteeUserName(companyUser.getName());
                                record1.setInviteeUserTel(companyUser.getUsername());
                                record1.setCompanyId(companyUser.getOwnCompanyId());
                                record1.setCompanyName(companyUser.getOwnCompanyName());
                                record1.setInviteCompanyId(hsmwHtnUser.getOwnCompanyId());
                                record1.setInviteCompanyName(hsmwHtnUser.getOwnCompanyName());
                                String recId = CommonUtils.generateId(record1);
                                record1.setId(recId);
                                records1.add(record1);
                                //搅拌站（或泵送公司）XXXXXXXXX（公司名称）的用户XXX（姓名+电话）邀请您所在的公司成为其合作泵送公司，负责其项目泵送，请尽快处理
                                message = "泵送公司" + hsmwHtnUser.getOwnCompanyName() + "的用户 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您所在的公司成为其合作泵送公司，负责其项目泵送，请尽快处理";
                                pushMessage = "以下公司用户" + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您成为该公司的合作泵送服务提供商";
                                String[] params = new String[]{hsmwHtnUser.getOwnCompanyName(), hsmwHtnUser.getName()};
                                SendSmsUtil.sendSmsWithParam(appId, appKey, params, companyInviteCorperate, smsSign, companyUser.getUsername());

                                String pushId = CommonUtils.generateId(pushInfo);
                                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                                webSocketReturnVO.setPushId(pushId);
                                webSocketReturnVO.setInvitedId(recId);
                                webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                                webSocketReturnVO.setMessage(pushMessage);
                                webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                                webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_CORPERATE.getValue());
                                webSocketReturnVO.setType(CommonConstants.YES);
                                webSocketReturnVO.setItemName(record.getItemName());
                                webSocketReturnVO.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                                webSocketReturnVO.setInviteeName(record.getInviteeUserName());
                                TencentPushUtils.htnPushInviteMsg(message, record.getInviteeUserTel(), webSocketReturnVO);
                                HsmwDriverNews news = new HsmwDriverNews();
                                news.setMessage(message);
                                news.setUserId(userId);
                                news.setAlreadyRead(CommonConstants.NO);
                                nesss.add(news);
                                pushInfo.setId(pushId);
                                pushInfo.setMessage(pushMessage);
                                pushInfo.setInvitedId(recId);
                                pushInfo.setForemanId(companyUser.getId());
                                pushInfo.setInvitedUserName(currentHtnUser.getUsername());
                                pushInfo.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                                pushInfo.setType(PushTypeEnum.COMPANY_INVITE_CORPERATE.getValue());
                                pushInfos.add(pushInfo);
                            }
                        }
                        hsmwInviteRecordService.saveBatch(records1);
                        hsmwDriverNewsService.saveBatch(nesss);
                        hsmwPushInfoService.saveBatch(pushInfos);
                        return new DataResponse(SUCCESS, "邀请成功！");
                    }
                }
                if (InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue().equals(record.getInviteType())) {
                    //泵送公司邀请的是多个合作公司
                    List<String> userIds = record.getUserIds();
                    if (CollectionUtil.isNotEmpty(userIds)) {
                        for (String userId : userIds) {
                            HsmwHtnUser companyUser = hsmwHtnUserService.getById(userId);
                            //如果已经存在该用户的邀请信息
                            List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                                    .in(HsmwInviteRecord::getInviteeUserId, userId)
                                    .eq(HsmwInviteRecord::getInviteCompanyId, companyUser.getOwnCompanyId())
                                    .eq(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue())
                                    .eq(HsmwInviteRecord::getInviteUserId, currentHtnUser.getId()));
                            if (CollectionUtil.isNotEmpty(records)) {
                                HsmwInviteRecord inviteRecord = records.get(0);
                                //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                                if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                                    return new DataResponse(SUCCESS, "邀请成功！");
                                }
                                //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                                if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                                    inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                                    hsmwInviteRecordMapper.updateById(inviteRecord);
                                    return DataResponse.success(CommonConstants.ResponseStatus.INVITE_SUCCESS.getCode());
                                }
                                //当出现已经邀请成功 的记录时 ， 不进行任何操作
                                if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                                    return new DataResponse(SUCCESS, "邀请成功！");
                                }
                            }

                            //添加推送的信息
                            HsmwPushInfo pushInfo = new HsmwPushInfo();
                            HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                            HsmwInviteRecord record1 = new HsmwInviteRecord();
                            BeanUtil.copyProperties(record, record1);
                            record1.setInviteType(InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue());
                            record1.setInviteUserId(currentHtnUser.getId());
                            record1.setInviteUserName(currentHtnUser.getName());
                            record1.setInviteUserTel(currentHtnUser.getUsername());
                            record1.setInviteeUserTel(companyUser.getUsername());
                            record1.setInviteeUserId(companyUser.getId());
                            record1.setInviteeUserName(companyUser.getName());
                            record1.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                            record1.setCompanyId(companyUser.getOwnCompanyId());
                            record1.setCompanyName(companyUser.getOwnCompanyName());
                            record1.setInviteCompanyId(hsmwHtnUser.getOwnCompanyId());
                            record1.setInviteCompanyName(hsmwHtnUser.getOwnCompanyName());
                            String recordId = CommonUtils.generateId(record1);
                            record1.setId(recordId);
                            //①搅拌站XXXXXX（搅拌站名称）的用户XXX（姓名+电话）邀请您所在的公司成为其自营泵送公司，负责其项目泵送，请尽快处理
                            message = "搅拌站" + hsmwHtnUser.getOwnCompanyName() + "的用户 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您所在的公司成为其合作泵送公司，负责其项目泵送，请尽快处理";
                            pushMessage = "以下公司用户" + hsmwHtnUser.getName() + hsmwHtnUser.getUsername() + "邀请您成为该公司的合作泵送服务提供商";
                            String[] params = new String[]{hsmwHtnUser.getOwnCompanyName(), hsmwHtnUser.getName()};
                            SendSmsUtil.sendSmsWithParam(appId, appKey, params, concreteInviteCorperate, smsSign, companyUser.getUsername());
                            String pushId = CommonUtils.generateId(pushInfo);
                            WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                            webSocketReturnVO.setPushId(pushId);
                            webSocketReturnVO.setInvitedId(recordId);
                            webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                            webSocketReturnVO.setMessage(pushMessage);
                            webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                            webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_CORPERATE.getValue());
                            webSocketReturnVO.setType(CommonConstants.YES);
                            webSocketReturnVO.setItemName(record.getItemName());
                            webSocketReturnVO.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                            TencentPushUtils.htnPushInviteMsg(message, record.getInviteeUserTel(), webSocketReturnVO);
                            hsmwDriverNews.setMessage(message);
                            hsmwDriverNews.setUserId(userId);
                            hsmwDriverNews.setAlreadyRead(NO);
                            hsmwDriverNewsService.save(hsmwDriverNews);
                            pushInfo.setId(pushId);
                            pushInfo.setType(PushTypeEnum.CONCRETE_INVITE_CORPERATE.getValue());
                            pushInfo.setMessage(pushMessage);
                            pushInfo.setInvitedId(recordId);
                            pushInfo.setForemanId(record1.getInviteeUserId());
                            pushInfo.setInvitedUserName(currentHtnUser.getUsername());
                            pushInfo.setInvitedName(currentHtnUser.getName());
                            pushInfo.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                            hsmwPushInfoService.save(pushInfo);
                            hsmwInviteRecordMapper.insert(record1);
                        }
                    }
                    hsmwInviteRecordService.saveBatch(records1);
                    hsmwDriverNewsService.saveBatch(nesss);
                    return new DataResponse(SUCCESS, "邀请成功！");
                }
                return new DataResponse(SUCCESS, "邀请成功！");
            }
        }
        if (InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue().equals(record.getInviteType())) {
            //添加推送的信息
            HsmwPushInfo pushInfo = new HsmwPushInfo();
            HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
            String message = "";
            String pushMessage = "";
            if (InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue().equals(record.getInviteType())) {
                HsmwHtnUser inviteeUser = hsmwHtnUserService.getById(inviteeUserId);
                //如果已经存在该用户的邀请信息
                List<HsmwInviteRecord> records = hsmwInviteRecordMapper.selectList(new LambdaQueryWrapper<HsmwInviteRecord>()
                        .eq(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue())
                        .eq(HsmwInviteRecord::getInviteUserId, currentHtnUser.getId()));
                if (CollectionUtil.isNotEmpty(records)) {
                    for(HsmwInviteRecord recordInfo :records){
                        hsmwPushInfoService.remove(new LambdaQueryWrapper<HsmwPushInfo>()
                                .eq(HsmwPushInfo::getInvitedId, recordInfo.getId()));
                        hsmwInviteRecordMapper.deleteById(recordInfo.getId());
                    }
                }
                pushInfo.setType(PushTypeEnum.COMPANY_INVITE_DISPATCH.getValue());
                //①搅拌站XXXXXX（搅拌站名称）的用户XXX（姓名+电话）②邀请您所在的公司成为其合作泵送公司，负责其项目泵送，请尽快处理
                message = "搅拌站" + hsmwHtnUser.getOwnCompanyName() + "的用户 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您所在的公司成为其自营泵送公司，负责其项目泵送，请尽快处理";
                pushMessage = "以下公司用户" + hsmwHtnUser.getName() + hsmwHtnUser.getUsername() + "邀请您成为该公司的自营泵送服务提供商";
                String[] params = new String[]{hsmwHtnUser.getOwnCompanyName(), hsmwHtnUser.getName()};
                SendSmsUtil.sendSmsWithParam(appId, appKey, params, concreteInviteSelfrun, smsSign, inviteeUser.getUsername());

                record.setInviteUserId(currentHtnUser.getId());
                record.setInviteUserName(currentHtnUser.getName());
                record.setInviteUserTel(currentHtnUser.getUsername());
                record.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                record.setCompanyId(inviteeUser.getOwnCompanyId());
                record.setCompanyName(inviteeUser.getOwnCompanyName());
                record.setInviteCompanyId(hsmwHtnUser.getOwnCompanyId());
                record.setInviteCompanyName(hsmwHtnUser.getOwnCompanyName());
                String recordId = CommonUtils.generateId(record);
                record.setId(recordId);
                pushInfo.setType(PushTypeEnum.CONCRETE_INVITE_SELFRUN.getValue());
                pushInfo.setMessage(pushMessage);
                pushInfo.setInvitedId(recordId);
                pushInfo.setForemanId(record.getInviteeUserId());
                pushInfo.setInvitedUserName(currentHtnUser.getUsername());
                pushInfo.setInvitedName(currentHtnUser.getName());
                pushInfo.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                String pushId = CommonUtils.generateId(pushInfo);
                pushInfo.setId(pushId);
                hsmwPushInfoService.save(pushInfo);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushId(pushId);
                webSocketReturnVO.setInvitedId(recordId);
                webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                webSocketReturnVO.setMessage(pushMessage);
                webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_SELFRUN.getValue());
                webSocketReturnVO.setType(CommonConstants.YES);
                webSocketReturnVO.setItemName(record.getItemName());
                webSocketReturnVO.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteeUserTel(), webSocketReturnVO);

                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteeUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                hsmwInviteRecordMapper.insert(record);
            }
        }
        //项目邀请搅拌站
        if (InviteTypeEnum.ITEM_INVITE_CONCRETE.getValue().equals(record.getInviteType())) {
            List<String> plantIds = record.getPlantIds();
            if(CollectionUtil.isNotEmpty(plantIds) && plantIds.size()>0){
                for(String plantId: plantIds){
                    //当选择的搅拌站是自己所属的公司的时候 直接生产邀请记录，并新增 项目和搅拌站的关联关系
                    if (StringUtils.isNotBlank(hsmwHtnUser.getOwnCompanyId()) &&  hsmwHtnUser.getOwnCompanyId().equals(plantId)){
                        HsmwInviteRecord recordInfo = new HsmwInviteRecord();
                        HsmwConcretePlantInfo info = hsmwConcretePlantInfoMapper.selectById(plantId);
                        BeanUtil.copyProperties(record, recordInfo);
                        recordInfo.setInviteType(InviteTypeEnum.ITEM_INVITE_CONCRETE.getValue());
                        recordInfo.setInviteUserId(currentHtnUser.getId());
                        recordInfo.setInviteUserName(currentHtnUser.getName());
                        recordInfo.setInviteUserTel(currentHtnUser.getUsername());
                        recordInfo.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                        recordInfo.setPlantId(plantId);
                        recordInfo.setPlantName(info.getPlantName());
                        recordInfo.setInviteCompanyId(hsmwHtnUser.getOwnCompanyId());
                        recordInfo.setInviteCompanyName(hsmwHtnUser.getOwnCompanyName());
                        String recordId = CommonUtils.generateId(recordInfo);
                        recordInfo.setId(recordId);
                        hsmwInviteRecordMapper.insert(recordInfo);

                        HsmwItemConcreteRelation relation = new HsmwItemConcreteRelation();
                        relation.setConcreteId(plantId);
                        relation.setConcreteName(info.getPlantName());
                        relation.setItemId(record.getItemId());
                        relation.setItemName(record.getItemName());
                        hsmwItemConcreteRelationService.save(relation);
                        continue;
                    }
                    List<HsmwInviteRecord> records = hsmwInviteRecordService.list(new LambdaQueryWrapper<HsmwInviteRecord>()
                            .eq(HsmwInviteRecord::getPlantId,plantId)
                            .eq(HsmwInviteRecord::getItemId,record.getItemId())
                            .eq(HsmwInviteRecord::getInviteType,InviteTypeEnum.ITEM_INVITE_CONCRETE.getValue()));
                    if (CollectionUtil.isNotEmpty(records)) {
                        HsmwInviteRecord inviteRecord = records.get(0);
                        //当出现邀请中的记录时，不进行任何操作，直接返回邀请成功
                        if (InviteStatusEnum.ON_PROCESS.getValue().equals(inviteRecord.getInviteStatus())) {
                            return new DataResponse(REPETE_INVITE, "已邀请过该用户，请勿重复邀请！");
                        }
                        //当出现已经拒绝的记录时 ， 将邀请记录改为邀请中， 直接返回邀请成功
                        if (InviteStatusEnum.REJECTED.getValue().equals(inviteRecord.getInviteStatus())) {
                            inviteRecord.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                            hsmwInviteRecordMapper.updateById(inviteRecord);
                            return new DataResponse(SUCCESS, "邀请成功！");
                        }
                        //当出现已经邀请成功 的记录时 ， 不进行任何操作
                        if (InviteStatusEnum.RECEIVED.getValue().equals(inviteRecord.getInviteStatus())) {
                            return new DataResponse(ITEM_INVITE_CONCRETE_REPETE, "该搅拌站已存在，无需重复邀请！");
                        }
                    }
                    //查询搅拌站的负责人
                    List<HsmwHtnUser> fzr = hsmwHtnUserService.list(new LambdaQueryWrapper<HsmwHtnUser>()
                            .eq(HsmwHtnUser::getOwnCompanyId,plantId)
                            .eq(HsmwHtnUser::getType,HtnUserTypeEnum.CONCRETE.getValue())
                            .eq(HsmwHtnUser::getRoleType,HtnUserRoleEnum.LEADING.getValue()));
                    if(CollectionUtil.isNotEmpty(fzr)){
                        HsmwHtnUser companyUser = fzr.get(0);
                        String message = "";
                        String pushMessage = "";
                        //添加推送的信息
                        HsmwPushInfo pushInfo = new HsmwPushInfo();
                        HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                        HsmwInviteRecord recordInfo = new HsmwInviteRecord();
                        BeanUtil.copyProperties(record, recordInfo);
                        recordInfo.setInviteType(InviteTypeEnum.ITEM_INVITE_CONCRETE.getValue());
                        recordInfo.setInviteUserId(currentHtnUser.getId());
                        recordInfo.setInviteUserName(currentHtnUser.getName());
                        recordInfo.setInviteUserTel(currentHtnUser.getUsername());
                        recordInfo.setInviteeUserTel(companyUser.getUsername());
                        recordInfo.setInviteeUserId(companyUser.getId());
                        recordInfo.setInviteeUserName(companyUser.getName());
                        recordInfo.setInviteStatus(InviteStatusEnum.ON_PROCESS.getValue());
                        recordInfo.setPlantId(companyUser.getOwnCompanyId());
                        recordInfo.setPlantName(companyUser.getOwnCompanyName());
                        recordInfo.setInviteCompanyId(hsmwHtnUser.getOwnCompanyId());
                        recordInfo.setInviteCompanyName(hsmwHtnUser.getOwnCompanyName());
                        String recordId = CommonUtils.generateId(recordInfo);
                        recordInfo.setId(recordId);

                        //①搅拌站XXXXXX（搅拌站名称）的用户XXX（姓名+电话）邀请您所在的公司成为其自营泵送公司，负责其项目泵送，请尽快处理
                        message = hsmwHtnUser.getOwnCompanyName() + "公司 " + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您成为"+record.getItemName() +"项目的混凝土提供方，请前往APP处理";
                        if(HtnUserTypeEnum.CONCRETE.getValue().equals(hsmwHtnUser.getType())){
                            pushMessage ="搅拌站";
                        }else {
                            pushMessage ="泵送公司";
                        }
                        pushMessage += hsmwHtnUser.getOwnCompanyName() + "用户" + hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername() + "邀请您加入以下项目,成为混凝土提供方";
                        String[] params = new String[]{hsmwHtnUser.getOwnCompanyName(),  hsmwHtnUser.getName() + " " + hsmwHtnUser.getUsername(),record.getItemName()};
                        SendSmsUtil.sendSmsWithParam(appId, appKey, params, itemInviteConcrete, smsSign, companyUser.getUsername());
                        String pushId = CommonUtils.generateId(pushInfo);
                        WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                        webSocketReturnVO.setPushId(pushId);
                        webSocketReturnVO.setInvitedId(recordId);
                        webSocketReturnVO.setInvitedUserName(hsmwHtnUser.getName());
                        webSocketReturnVO.setMessage(pushMessage);
                        webSocketReturnVO.setInvitedName(hsmwHtnUser.getName());
                        webSocketReturnVO.setPushType(PushTypeEnum.ITEM_INVITE_CONCRETE.getValue());
                        webSocketReturnVO.setType(CommonConstants.YES);
                        webSocketReturnVO.setItemName(record.getItemName());
                        webSocketReturnVO.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                        TencentPushUtils.htnPushInviteMsg(message, companyUser.getUsername(), webSocketReturnVO);

                        hsmwDriverNews.setMessage(message);
                        hsmwDriverNews.setUserId(companyUser.getId());
                        hsmwDriverNews.setAlreadyRead(NO);
                        hsmwDriverNewsService.save(hsmwDriverNews);
                        pushInfo.setId(pushId);
                        pushInfo.setType(PushTypeEnum.ITEM_INVITE_CONCRETE.getValue());
                        pushInfo.setMessage(pushMessage);
                        pushInfo.setInvitedId(recordId);
                        pushInfo.setForemanId(recordInfo.getInviteeUserId());
                        pushInfo.setInvitedUserName(currentHtnUser.getUsername());
                        pushInfo.setInvitedName(currentHtnUser.getName());
                        pushInfo.setInvitedCompanyName(hsmwHtnUser.getOwnCompanyName());
                        pushInfo.setInvitedItemName(recordInfo.getItemName());
                        hsmwPushInfoService.save(pushInfo);
                        hsmwInviteRecordMapper.insert(recordInfo);
                    }
                }
            }
        }
        return new DataResponse(SUCCESS,"成功");
    }

    public DataResponse reInviteInfo(HsmwInviteRecord record) {
        HsmwInviteRecord inviteRecord = hsmwInviteRecordService.getById(record.getId());
        HsmwPushInfo pushInfo = hsmwPushInfoService.getOne(new LambdaQueryWrapper<HsmwPushInfo>()
                .eq(HsmwPushInfo::getInvitedId, record.getId()));
        if(StringUtils.isNotNull(pushInfo)){
            hsmwPushInfoService.removeById(pushInfo.getId());
        }
        hsmwInviteRecordMapper.deleteById(inviteRecord.getId());

        //重新走新增的逻辑
        List<String> userList = new ArrayList<>();
        userList.add(inviteRecord.getInviteeUserId());
        inviteRecord.setUserIds(userList);
        List<String> plantIds = new ArrayList<>();
        plantIds.add(inviteRecord.getPlantId());
        inviteRecord.setPlantIds(plantIds);
        return saveOrUpdateInviteInfo(inviteRecord);
    }

    public DataResponse removeStuff(HsmwInviteRecord record) {
        HsmwInviteRecord recordInfo = hsmwInviteRecordMapper.selectById(record.getId());
        if(StringUtils.isNull(recordInfo)){
            return new DataResponse(SUCCESS);
        }
        HsmwPushInfo pushInfo = hsmwPushInfoService.getOne(new LambdaQueryWrapper<HsmwPushInfo>()
                .eq(HsmwPushInfo::getInvitedId, recordInfo.getId()));
        //公司或者搅拌站 邀请调度的移除
        if (InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(recordInfo.getInviteType())
                || InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue().equals(recordInfo.getInviteType())) {
            HashMap params = new HashMap();
            params.put("userId",recordInfo.getInviteeUserId());
            Integer goingCount =hsmwOrderMapper.selectGoingOrderCount(params);
            if(StringUtils.isNotNull(goingCount) && goingCount>0){
                return new DataResponse(HAS_GOING_ORDER, "有进行中订单，无法删除！");
            }
            //移除调度的身份
            HashMap param = new HashMap();
            param.put("userId", recordInfo.getInviteeUserId());
            hsmwHtnUserService.removeStuff(param);
            //删除登录信息
            //redisTemplate.delete(recordInfo.getInviteeUserId());

            HsmwPushInfo one = hsmwPushInfoService.getOne(new LambdaQueryWrapper<HsmwPushInfo>()
                    .eq(HsmwPushInfo::getInvitedId, record.getId()));
            //邀请人信息
            HsmwHtnUser inviteUser = hsmwHtnUserService.getHsmwUserById(recordInfo.getInviteUserId());
            String message = "您已被公司负责人移出" + inviteUser.getOwnCompanyName();
            // 后发送推送
            WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
            webSocketReturnVO.setPushId(one.getId());
            webSocketReturnVO.setInvitedId(one.getInvitedId());
            webSocketReturnVO.setInvitedUserName(one.getInvitedUserName());
            webSocketReturnVO.setMessage(message);
            webSocketReturnVO.setInvitedCompanyName(one.getInvitedCompanyName());
            webSocketReturnVO.setInvitedName(one.getInvitedName());
            webSocketReturnVO.setPushType(one.getType());
            webSocketReturnVO.setType(CommonConstants.NO);
            webSocketReturnVO.setItemName(one.getInvitedItemName());
            TencentPushUtils.htnPushInviteMsg(message, recordInfo.getInviteeUserTel(), webSocketReturnVO);
            HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
            hsmwDriverNews.setMessage(message);
            hsmwDriverNews.setUserId(recordInfo.getInviteeUserId());
            hsmwDriverNews.setAlreadyRead(NO);
            hsmwDriverNewsService.save(hsmwDriverNews);

        }
        //公司、搅拌站 邀请合作公司和自营公司
        if (InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue().equals(recordInfo.getInviteType())
                || InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue().equals(recordInfo.getInviteType())
                || InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue().equals(recordInfo.getInviteType())) {
            LambdaQueryWrapper<HsmwCorporationCompany> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if(InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue().equals(recordInfo.getInviteType())){
                lambdaQueryWrapper.eq(HsmwCorporationCompany::getCompanyId, recordInfo.getInviteCompanyId());
            }else {
                lambdaQueryWrapper.eq(HsmwCorporationCompany::getConcretePlantId, recordInfo.getInviteCompanyId());
            }
            lambdaQueryWrapper.eq(HsmwCorporationCompany::getCorperateCompanyId, recordInfo.getCompanyId());
            //将合作公司记录表中数据删掉
            hsmwCorporationCompanyMapper.delete(lambdaQueryWrapper);
        }
        //公司、搅拌站 邀请工长和项目负责人
        if (InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue().equals(recordInfo.getInviteType())
                || InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue().equals(recordInfo.getInviteType())
                || InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue().equals(recordInfo.getInviteType())
                || InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue().equals(recordInfo.getInviteType())
                || InviteTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue().equals(recordInfo.getInviteType())
        ) {
            //移除后 如果该用户是最后一个被移除项目的工长那么 需要将该用户的用户类型该为普通用户
            String flag = "0";
            //将项目和工长关联记录表  中数据删掉
            hsmwForcemanItemMapper.delete(new LambdaQueryWrapper<HsmwForcemanItem>()
                    .eq(HsmwForcemanItem::getItemId, recordInfo.getItemId())
                    .eq(HsmwForcemanItem::getForcemanId, recordInfo.getInviteeUserId()));
            //删除登录信息
            //redisTemplate.delete(recordInfo.getInviteeUserId());
            HsmwHtnUser inviteUserInfo = hsmwHtnUserService.getHsmwUserById(recordInfo.getInviteUserId());

            String message = "";
            if (HtnUserTypeEnum.CONCRETE.getValue().equals(inviteUserInfo.getType())) {
                message = "您被搅拌站负责人用户" + inviteUserInfo.getName() + "移出" + recordInfo.getItemName() + "项目";
            } else if(HtnUserTypeEnum.COMPANY.getValue().equals(inviteUserInfo.getType())) {
                message = "您被泵送公司用户" + inviteUserInfo.getName() + "移出" + recordInfo.getItemName() + "项目";
            }else {
                message = "您被项目负责人用户" + inviteUserInfo.getName() + "移出" + recordInfo.getItemName() + "项目";
            }
            HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
            hsmwDriverNews.setMessage(message);
            hsmwDriverNews.setUserId(recordInfo.getInviteeUserId());
            hsmwDriverNews.setAlreadyRead(NO);
            hsmwDriverNewsService.save(hsmwDriverNews);

            WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
            webSocketReturnVO.setPushId(pushInfo.getId());
            webSocketReturnVO.setInvitedId(pushInfo.getInvitedId());
            webSocketReturnVO.setInvitedUserName(pushInfo.getInvitedUserName());
            webSocketReturnVO.setMessage(message);
            webSocketReturnVO.setInvitedCompanyName(pushInfo.getInvitedCompanyName());
            webSocketReturnVO.setInvitedName(pushInfo.getInvitedName());
            webSocketReturnVO.setPushType(pushInfo.getType());
            webSocketReturnVO.setType(CommonConstants.NO);
            List<HsmwForcemanItem> items = hsmwForcemanItemMapper.selectList(new LambdaQueryWrapper<HsmwForcemanItem>()
                    .eq(HsmwForcemanItem::getForcemanId, recordInfo.getInviteeUserId())
                    .ne(HsmwForcemanItem::getItemId, recordInfo.getItemId()));
            if (CollectionUtil.isEmpty(items)) {
                HashMap param = new HashMap();
                param.put("userId", recordInfo.getInviteeUserId());
                hsmwHtnUserService.removeStuff(param);
                flag = "1";
            }
            webSocketReturnVO.setStatus(flag);
            webSocketReturnVO.setItemName(pushInfo.getInvitedItemName());
            TencentPushUtils.htnPushInviteMsg(message, recordInfo.getInviteeUserTel(), webSocketReturnVO);

        }
        if(InviteTypeEnum.ITEM_INVITE_CONCRETE.getValue().equals(recordInfo.getInviteType())){
            //将项目搅拌站的关联表的数据删掉
            hsmwItemConcreteRelationMapper.delete(new LambdaQueryWrapper<HsmwItemConcreteRelation>()
                    .eq(HsmwItemConcreteRelation::getItemId,recordInfo.getItemId())
                    .eq(HsmwItemConcreteRelation::getConcreteId,recordInfo.getPlantId()));
            hsmwInviteRecordMapper.deleteById(recordInfo.getId());
        }
        //删除邀请记录
        hsmwInviteRecordMapper.deleteById(record.getId());
        return new DataResponse();
    }


    public DataResponse findDriverReportPushInfoByOrderId(String orderId) {
        List<HsmwPushInfo> ones = hsmwPushInfoService.list(new LambdaQueryWrapper<HsmwPushInfo>()
                .eq(HsmwPushInfo::getOrderId, orderId)
                .eq(HsmwPushInfo::getStatus, NO)
                .eq(HsmwPushInfo::getType, "DRIVER_REPORT"));
        List<WebSocketReturnVO> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(ones)) {
            for (HsmwPushInfo one : ones) {
                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushId(one.getId());
                webSocketReturnVO.setOrderId(one.getOrderId());
                webSocketReturnVO.setRelationId(one.getRelationId());
                webSocketReturnVO.setMessage(one.getMessage());
                webSocketReturnVO.setReportId(one.getReportId());
                list.add(webSocketReturnVO);
            }
        }
        return new DataResponse(list);
    }

    @Transactional
    public DataResponse receiveOrRejectInviteInfo(String pushId, String operType) {
        LoginUser user = currentHsmwUserUtils.getCurrentUserInfo();
        HsmwPushInfo pushInfo = hsmwPushInfoService.getById(pushId);
        if (StringUtils.isNull(pushInfo)) {
            return new DataResponse(CommonConstants.ResponseStatus.INVITE_STATUS_CHANGE, "邀请状态已经改变，请刷新页面！");
        }
        pushInfo.setStatus("1");
        pushInfo.setUpdateTime(LocalDateTime.now());
        hsmwPushInfoService.updateById(pushInfo);

        HsmwInviteRecord record = hsmwInviteRecordMapper.selectById(pushInfo.getInvitedId());
        HsmwHtnUser inviteUserInfo = hsmwHtnUserService.getHsmwUserById(record.getInviteUserId());
        HsmwHtnUser inviteeUser = hsmwHtnUserService.getById(record.getInviteeUserId());
        if(!InviteStatusEnum.ON_PROCESS.getValue().equals(record.getInviteStatus())){
            return new DataResponse(CommonConstants.ResponseStatus.INVITE_STATUS_CHANGE, "邀请状态已经改变，请刷新页面！");
        }
        // 3 搅拌站邀请自营泵送公司
        if (InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue().equals(record.getInviteType())) {
            if (CommonConstants.YES.equals(operType)) {
                record.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);
                HsmwCorporationCompany corporationCompany = new HsmwCorporationCompany();
                corporationCompany.setConcretePlantId(inviteUserInfo.getOwnCompanyId());
                corporationCompany.setConcretePlantName(inviteUserInfo.getOwnCompanyName());
                corporationCompany.setCorperateCompanyId(inviteeUser.getOwnCompanyId());
                corporationCompany.setCorperateCompanyName(inviteeUser.getOwnCompanyName());
                corporationCompany.setCorperateType(CorperateTypeEnum.SELFRUN.getValue());
                corporationCompany.setHandleUserId(user.getId());
                corporationCompany.setHandleUserName(user.getName());
                hsmwCorporationCompanyMapper.insert(corporationCompany);

                //同意邀请后 直接将其他租赁公司或者搅拌站的邀请的这个人的邀请信息给拒绝。
                HsmwInviteRecord recordInfo = new HsmwInviteRecord();
                recordInfo.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                recordInfo.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.update(recordInfo, new LambdaQueryWrapper<HsmwInviteRecord>()
                        .eq(HsmwInviteRecord::getInviteeUserId, record.getInviteeUserId())
                        .eq(HsmwInviteRecord::getInviteStatus, InviteStatusEnum.ON_PROCESS.getValue())
                        .ne(HsmwInviteRecord::getId, record));
                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已同意成为您的自营泵送公司";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_SELFRUN.getValue());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);

                return new DataResponse();
            } else {
                record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已拒绝成为您的自营泵送公司";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                String messages = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已拒绝成为您的自营泵送公司";
                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_SELFRUN.getValue());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(messages, record.getInviteUserTel(), webSocketReturnVO);

                return new DataResponse();
            }
        }
        //4 搅拌站邀请合作公司
        if (InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue().equals(record.getInviteType())) {
            if (CommonConstants.YES.equals(operType)) {
                record.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);
                HsmwCorporationCompany corporationCompany = new HsmwCorporationCompany();
                corporationCompany.setConcretePlantId(inviteUserInfo.getOwnCompanyId());
                corporationCompany.setConcretePlantName(inviteUserInfo.getOwnCompanyName());
                corporationCompany.setCorperateCompanyId(inviteeUser.getOwnCompanyId());
                corporationCompany.setCorperateCompanyName(inviteeUser.getOwnCompanyName());
                corporationCompany.setCorperateType(CorperateTypeEnum.CORPERATE.getValue());
                corporationCompany.setHandleUserId(user.getId());
                corporationCompany.setHandleUserName(user.getName());
                hsmwCorporationCompanyMapper.insert(corporationCompany);
                //同意邀请后 不需要 将其他租赁公司或者搅拌站的邀请的这个人的邀请信息给拒绝。

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已同意成为您的合作泵送公司";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_CORPERATE.getValue());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);
                return new DataResponse();
            } else {
                record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已拒绝成为您的合作泵送公司";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_CORPERATE.getValue());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);

                return new DataResponse();
            }
        }
        //2 公司邀请合作公司
        if (InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue().equals(record.getInviteType())) {
            if (CommonConstants.YES.equals(operType)) {
                record.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus(CommonConstants.YES);
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                HsmwCorporationCompany corporationCompany = new HsmwCorporationCompany();
                corporationCompany.setCompanyId(inviteUserInfo.getOwnCompanyId());
                corporationCompany.setCompanyName(inviteUserInfo.getOwnCompanyName());
                corporationCompany.setCorperateCompanyId(inviteeUser.getOwnCompanyId());
                corporationCompany.setCorperateCompanyName(inviteeUser.getOwnCompanyName());
                corporationCompany.setCorperateType(CorperateTypeEnum.CORPERATE.getValue());
                corporationCompany.setHandleUserId(user.getId());
                corporationCompany.setHandleUserName(user.getName());
                hsmwCorporationCompanyMapper.insert(corporationCompany);
                //同意邀请后 不需要 将其他租赁公司或者搅拌站的邀请的这个人的邀请信息给拒绝。

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已同意成为您的合作泵送公司";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_CORPERATE.getValue());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);


                return new DataResponse();
            } else {
                record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                String message = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已拒绝成为您的合作泵送公司";
                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_CORPERATE.getValue());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);
                return new DataResponse();
            }
        }
        //9 搅拌站邀请调度 或者  1 公司邀请项目负责人的 时候的同意
        if (InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(record.getInviteType())
                || InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue().equals(record.getInviteType())) {
            if (CommonConstants.YES.equals(operType)) {
                if(!HtnUserTypeEnum.NORMAL.getValue().equals(inviteeUser.getType())){
                    record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                    record.setRejectOrReceivedTime(LocalDateTime.now());
                    hsmwInviteRecordMapper.updateById(record);
                    return new DataResponse(CommonConstants.ResponseStatus.REGISTER_OTHER_ROLE, "该账号已有其他角色！");
                }
                if (InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(record.getInviteType())) {
                    inviteeUser.setType(HtnUserTypeEnum.CONCRETE.getValue());
                } else {
                    inviteeUser.setType(HtnUserTypeEnum.COMPANY.getValue());
                }
                //同意的话 将被邀请用户的用户类型改为
                inviteeUser.setRoleType(HtnUserRoleEnum.DISPATCH.getValue());
                inviteeUser.setOwnCompanyId(inviteUserInfo.getOwnCompanyId());
                inviteeUser.setOwnCompanyName(inviteUserInfo.getOwnCompanyName());
                inviteeUser.setName(record.getInviteeUserName());
                hsmwHtnUserService.updateById(inviteeUser);
                record.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                //同意邀请后 直接将其他租赁公司或者搅拌站的邀请的这个人的邀请信息给拒绝。
                HsmwInviteRecord recordInfo = new HsmwInviteRecord();
                recordInfo.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                recordInfo.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.update(recordInfo, new LambdaQueryWrapper<HsmwInviteRecord>()
                        .eq(HsmwInviteRecord::getInviteeUserId, record.getInviteeUserId())
                        .eq(HsmwInviteRecord::getInviteStatus, InviteStatusEnum.ON_PROCESS.getValue())
                        .ne(HsmwInviteRecord::getId, record.getId()));

                HashMap param = new HashMap();
                param.put("userId",record.getInviteeUserId());
                param.put("recordId",record.getId());
                hsmwPushInfoService.updateDispatchPushInfo(param);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                if(InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_DISPATCH.getValue());
                }
                if(InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_DISPATCH.getValue());
                }
                String message = "用户"+record.getInviteeUserName()+"已经同意您的调度邀请";
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);
                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);


                return new DataResponse();
            } else {
                record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus(CommonConstants.YES);
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                if(InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_DISPATCH.getValue());
                }
                if(InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_DISPATCH.getValue());
                }
                String message = "用户"+record.getInviteeUserName()+"已经拒绝您的调度邀请";
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);
                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);

            }
        }
        //9 搅拌站邀请调度 或者  1 公司邀请项目负责人的 时候的同意
        if (InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())
                || InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())) {
            if (CommonConstants.YES.equals(operType)) {
                //当该用户已经是 租赁公司或者 搅拌站用户了
                if(HtnUserTypeEnum.COMPANY.getValue().equals(inviteeUser.getType())|| HtnUserTypeEnum.CONCRETE.getValue().equals(inviteeUser.getType())){
                    record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                    record.setRejectOrReceivedTime(LocalDateTime.now());
                    hsmwInviteRecordMapper.updateById(record);
                    return new DataResponse(CommonConstants.ResponseStatus.REGISTER_OTHER_ROLE, "该账号已有其他角色！");
                }
                //当该用户已经是 工长用户
                if(HtnUserTypeEnum.FOREMAN.getValue().equals(inviteeUser)){
                    //判断该用户是不是该项目的工长或者负责人
                    List<HsmwForcemanItem> items = hsmwForcemanItemMapper.selectList(new LambdaQueryWrapper<HsmwForcemanItem>()
                            .eq(HsmwForcemanItem::getItemId,record.getItemId())
                            .eq(HsmwForcemanItem::getForcemanId,inviteeUser.getId()));
                    if(CollectionUtil.isNotEmpty(items)){
                        record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                        record.setRejectOrReceivedTime(LocalDateTime.now());
                        hsmwInviteRecordMapper.updateById(record);
                        return new DataResponse(CommonConstants.ResponseStatus.USER_EXISTS, "该用户已存在，无需重复邀请！");
                    }
                }
                //同意的话 将被邀请用户的用户类型改为 FOREMAN 和 项目负责人
                inviteeUser.setType(HtnUserTypeEnum.FOREMAN.getValue());
                //inviteeUser.setRoleType(ForemanRoleEnum.PROJECTMAN.getValue());
                inviteeUser.setName(record.getInviteeUserName());
                hsmwHtnUserService.updateById(inviteeUser);
                record.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);
                //同时 在工长和项目关联表中新增一条记录
                HsmwForcemanItem forcemanItem = new HsmwForcemanItem();
                forcemanItem.setItemId(record.getItemId());
                forcemanItem.setForcemanId(inviteeUser.getId());
                forcemanItem.setRoleType(ForemanRoleEnum.PROJECTMAN.getValue());
                hsmwForcemanItemMapper.insert(forcemanItem);
                //同意邀请后 不需要 其他租赁公司或者搅拌站的邀请的这个人的邀请信息给拒绝。
                //
                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "项目用户" + record.getInviteeUserName() + "同意加入您的" + record.getItemName() + "项目";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);

                //同意邀请后 同意成为工长或负责人时，邀请成为调度的弹窗全部按拒绝处理，项目邀请还存在
                HsmwInviteRecord recordInfo = new HsmwInviteRecord();
                recordInfo.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                recordInfo.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.update(recordInfo, new LambdaQueryWrapper<HsmwInviteRecord>()
                        .eq(HsmwInviteRecord::getInviteeUserId, record.getInviteeUserId())
                        .eq(HsmwInviteRecord::getInviteStatus, InviteStatusEnum.ON_PROCESS.getValue())
                        .in(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue(), InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue())
                        .ne(HsmwInviteRecord::getId, record.getId()));

                HashMap param = new HashMap();
                param.put("userId",record.getInviteeUserId());
                hsmwPushInfoService.updateForemanPushInfo(param);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                if(InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue());
                }
                if(InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue());
                }
                webSocketReturnVO.setItemName(record.getItemName());
                webSocketReturnVO.setItemId(record.getItemId());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);



                return new DataResponse();
            } else {
                record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "项目用户" + record.getInviteeUserName() + "拒绝加入您的" + record.getItemName() + "项目";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                if(InviteTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_PROJECTMAN.getValue());
                }
                if(InviteTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_PROJECTMAN.getValue());
                }
                webSocketReturnVO.setItemName(record.getItemName());
                webSocketReturnVO.setItemId(record.getItemId());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);


            }
        }
        //9 搅拌站邀请调度 或者  1 公司邀请调度的 时候的同意
        if (InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue().equals(record.getInviteType())
                || InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue().equals(record.getInviteType())
                || InviteTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue().equals(record.getInviteType())
        ) {
            if (CommonConstants.YES.equals(operType)) {
                //当该用户已经是 租赁公司或者 搅拌站用户了
                if(HtnUserTypeEnum.COMPANY.getValue().equals(inviteeUser.getType())|| HtnUserTypeEnum.CONCRETE.getValue().equals(inviteeUser.getType())){
                    record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                    record.setRejectOrReceivedTime(LocalDateTime.now());
                    hsmwInviteRecordMapper.updateById(record);
                    return new DataResponse(CommonConstants.ResponseStatus.REGISTER_OTHER_ROLE, "该账号已有其他角色！");
                }
                //当该用户已经是 工长用户
                if(HtnUserTypeEnum.FOREMAN.getValue().equals(inviteeUser)){
                    //判断该用户是不是该项目的工长或者负责人
                    List<HsmwForcemanItem> items = hsmwForcemanItemMapper.selectList(new LambdaQueryWrapper<HsmwForcemanItem>()
                            .eq(HsmwForcemanItem::getItemId,record.getItemId())
                            .eq(HsmwForcemanItem::getForcemanId,inviteeUser.getId()));
                    if(CollectionUtil.isNotEmpty(items)){

                        record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                        record.setRejectOrReceivedTime(LocalDateTime.now());
                        hsmwInviteRecordMapper.updateById(record);
                        return new DataResponse(CommonConstants.ResponseStatus.USER_EXISTS, "该用户已存在，无需重复邀请！");
                    }
                }

                //同意的话 将被邀请用户的用户类型改为 FOREMAN 和 项目负责人
                inviteeUser.setType(HtnUserTypeEnum.FOREMAN.getValue());
                //inviteeUser.setRoleType(ForemanRoleEnum.FOREMEN.getValue());
                inviteeUser.setName(record.getInviteeUserName());
                hsmwHtnUserService.updateById(inviteeUser);
                record.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);
                //同时 在工长和项目关联表中新增一条记录
                HsmwForcemanItem forcemanItem = new HsmwForcemanItem();
                forcemanItem.setItemId(record.getItemId());
                forcemanItem.setForcemanId(inviteeUser.getId());
                forcemanItem.setRoleType(ForemanRoleEnum.FOREMEN.getValue());
                hsmwForcemanItemMapper.insert(forcemanItem);
                //同意邀请后 不需要 其他租赁公司或者搅拌站的邀请的这个人的邀请信息给拒绝。

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "项目用户" + record.getInviteeUserName() + "同意加入您的" + record.getItemName() + "项目";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), null);


                //同意邀请后 同意成为工长或负责人时，邀请成为调度的弹窗全部按拒绝处理，项目邀请还存在
                HsmwInviteRecord recordInfo = new HsmwInviteRecord();
                recordInfo.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                recordInfo.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.update(recordInfo, new LambdaQueryWrapper<HsmwInviteRecord>()
                        .eq(HsmwInviteRecord::getInviteeUserId, record.getInviteeUserId())
                        .eq(HsmwInviteRecord::getInviteStatus, InviteStatusEnum.ON_PROCESS.getValue())
                        .in(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue(), InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue())
                        .ne(HsmwInviteRecord::getId, record.getId()));
                //将推送信息改为已读
                HashMap param = new HashMap();
                param.put("userId",record.getInviteeUserId());
                hsmwPushInfoService.updateForemanPushInfo(param);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                if(InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_FOREMAN.getValue());
                }
                if(InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_FOREMAN.getValue());
                }
                if(InviteTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.PROJECTMAN_INVITE_FOREMAN.getValue());
                }
                webSocketReturnVO.setItemName(record.getItemName());
                webSocketReturnVO.setItemId(record.getItemId());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);

                return new DataResponse();
            } else {
                record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "项目用户" + record.getInviteeUserName() + "拒绝加入您的" + record.getItemName() + "项目";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                if(InviteTypeEnum.COMPANY_INVITE_FOREMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.COMPANY_INVITE_FOREMAN.getValue());
                }
                if(InviteTypeEnum.CONCRETE_INVITE_FOREMAN.getValue().equals(record.getInviteType())){
                    webSocketReturnVO.setPushType(PushTypeEnum.CONCRETE_INVITE_FOREMAN.getValue());
                }
                webSocketReturnVO.setItemName(record.getItemName());
                webSocketReturnVO.setItemId(record.getItemId());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);
            }
        }
        //10   项目邀请搅拌站
        if (InviteTypeEnum.ITEM_INVITE_CONCRETE.getValue().equals(record.getInviteType())) {
            if (CommonConstants.YES.equals(operType)) {
                record.setInviteStatus(InviteStatusEnum.RECEIVED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus(CommonConstants.YES);
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                HsmwItemConcreteRelation relation = new HsmwItemConcreteRelation();
                relation.setItemId(record.getItemId());
                relation.setItemName(record.getItemName());
                relation.setConcreteId(record.getPlantId());
                relation.setConcreteName(record.getPlantName());
                hsmwItemConcreteRelationMapper.insert(relation);

                //同意邀请后 不需要 处理其他人的项目 邀请信息
                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "您邀请的搅拌站" + inviteeUser.getOwnCompanyName() + "已同意成为您的混凝土提供方";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);
                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.ITEM_INVITE_CONCRETE.getValue());
                webSocketReturnVO.setItemId(record.getItemId());
                webSocketReturnVO.setItemName(record.getItemName());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);

                return new DataResponse();
            } else {
                record.setInviteStatus(InviteStatusEnum.REJECTED.getValue());
                record.setRejectOrReceivedTime(LocalDateTime.now());
                hsmwInviteRecordMapper.updateById(record);
                pushInfo.setStatus("1");
                pushInfo.setUpdateTime(LocalDateTime.now());
                hsmwPushInfoService.updateById(pushInfo);

                HsmwDriverNews hsmwDriverNews = new HsmwDriverNews();
                String message = "您邀请的公司" + inviteeUser.getOwnCompanyName() + "已拒绝成为您的混凝土提供方";
                hsmwDriverNews.setMessage(message);
                hsmwDriverNews.setUserId(record.getInviteUserId());
                hsmwDriverNews.setAlreadyRead(NO);
                hsmwDriverNewsService.save(hsmwDriverNews);

                WebSocketReturnVO webSocketReturnVO = new WebSocketReturnVO();
                webSocketReturnVO.setPushType(PushTypeEnum.ITEM_INVITE_CONCRETE.getValue());
                webSocketReturnVO.setItemId(record.getItemId());
                webSocketReturnVO.setItemName(record.getItemName());
                webSocketReturnVO.setMessage("邀请反馈");
                TencentPushUtils.htnPushInviteMsg(message, record.getInviteUserTel(), webSocketReturnVO);
                return new DataResponse();
            }
        }

        return new DataResponse(SUCCESS,"成功");

    }


    public DataResponse concreteRechooseCompany(HsmwInviteRecord record) {
        HsmwHtnUser currentUser = currentHsmwUserUtils.getCurrentHtnUserInfo();
        //首选删除的之前的邀请记录
        HsmwInviteRecord inviteRecord = hsmwInviteRecordMapper.selectOne(new LambdaQueryWrapper<HsmwInviteRecord>()
                .eq(HsmwInviteRecord::getInviteType, InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue())
                .eq(HsmwInviteRecord::getInviteUserId, currentUser.getId()));
        //删除提醒表中的信息
        hsmwPushInfoService.remove(new LambdaQueryWrapper<HsmwPushInfo>()
                .eq(HsmwPushInfo::getInvitedId, inviteRecord.getId()));
        //删除 邀请信息的记录
        hsmwInviteRecordMapper.deleteById(record);
        HsmwLeasingCompany company = hsmwLeasingCompanyMapper.findLeasingCompanyById(record.getCompanyId());
        //删除 之前的合作公司记录
        hsmwCorporationCompanyMapper.delete(new LambdaQueryWrapper<HsmwCorporationCompany>()
                .eq(HsmwCorporationCompany::getCorperateType,CorperateTypeEnum.SELFRUN.getValue())
                .eq(HsmwCorporationCompany::getConcretePlantId,currentUser.getOwnCompanyId()));
        HashMap param =  new HashMap();
        param.put("concretePlantId",currentUser.getOwnCompanyId());
        hsmwCorporationCompanyMapper.deleteExistsCorps(param);





        //租赁公司的负责人
        HsmwHtnUser ownUser = hsmwHtnUserService.getOne(new LambdaQueryWrapper<HsmwHtnUser>()
                .eq(HsmwHtnUser::getType, HtnUserTypeEnum.COMPANY.getValue())
                .eq(HsmwHtnUser::getOwnCompanyId, company.getId())
                .eq(HsmwHtnUser::getRoleType, HtnUserRoleEnum.LEADING.getValue()));
        record.setInviteUserId(currentUser.getId());
        record.setInviteUserName(currentUser.getName());
        record.setInviteUserTel(currentUser.getUsername());
        record.setInviteType(InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue());
        if (StringUtils.isNotNull(ownUser)) {
            record.setInviteeUserId(ownUser.getId());
            record.setInviteeUserTel(ownUser.getUsername());
            record.setInviteeUserName(ownUser.getName());
        }
        hsmwInviteRecordMapper.insert(record);
        return new DataResponse();
    }

    public List<HsmwInviteRecord> queryInviteList(String type) {
        //当前用户
        LoginUser currentHtnUser = currentHsmwUserUtils.getCurrentUserInfo();
        //如果是员工的查询那么查询
        //查询出当前用户的负责人信息
        HashMap param = new HashMap();
        param.put("inviteType",type);
        if(InviteTypeEnum.COMPANY_INVITE_DISPATCH.getValue().equals(type) || InviteTypeEnum.CONCRETE_INVITE_DISPATCH.getValue().equals(type)|| InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue().equals(type)
                || InviteTypeEnum.COMPANY_INVITE_CORPERATE.getValue().equals(type)
                || InviteTypeEnum.CONCRETE_INVITE_CORPERATE.getValue().equals(type)
                || InviteTypeEnum.CONCRETE_INVITE_SELFRUN.getValue().equals(type)
        ) {
            HsmwHtnUser hsmwHtnUser = currentHsmwUserUtils.getCurrentHtnUserInfo();
            param.put("companyId",hsmwHtnUser.getOwnCompanyId());
        }else {
            param.put("userId",currentHtnUser.getId());
        }
        List<HsmwInviteRecord> list =  hsmwInviteRecordMapper.queryInviteList(param);
        if(CollectionUtil.isNotEmpty(list)){
            for (HsmwInviteRecord record :list){
                if (InviteStatusEnum.RECEIVED.getValue().equals(record.getInviteStatus())){
                    HsmwHtnUser htnUser = hsmwHtnUserService.getHsmwUserById(record.getInviteeUserId());
                    record.setInviteeUserName(htnUser.getName());
                }
            }
        }
        return list;
    }
}
